﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoundController : MonoBehaviour
{
    FirstController controller;
    IActionManager actionManager;       // 动作管理者
    DiskFactory diskFactory;            // 飞碟工厂
    UserGUI userGUI;
    int[] roundDisks;           // 对应轮次的飞碟数量
    int mode;                   // 当前模式，简单-0，正常-1，困难-2，或者无限模式-3
    int points;                 // 当前分数
    int round;                  // 当前轮次
    int sendCount;              // 当前已发送的飞碟数量
    float sendTime;             // 发送时间

    void Start()
    {
        controller = (FirstController)SSDirector.GetInstance().CurrentScenceController;
        actionManager = Singleton<ActionManager>.Instance;
        diskFactory = Singleton<DiskFactory>.Instance;
        userGUI = Singleton<UserGUI>.Instance;
        roundDisks = new int[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
        mode = 1;
        points = 0;
        round = 1;
        sendCount = 0;
        sendTime = 0;
    }

    public void Reset()
    {
        mode = 1;
        points = 0;
        round = 1;
        sendCount = 0;
        sendTime = 0;
    }

    public void Record(DiskData disk)
    {
        points += disk.points;
    }

    public int GetPoints()
    {
        return points;
    }

    public void SetMode(int mode)
    {
        this.mode = mode;
    }

    public void SetFlyMode(int flyMode)
    {
        actionManager = flyMode == 1 ? Singleton<PhysisActionManager>.Instance : Singleton<ActionManager>.Instance as IActionManager;
    }

    public void SendDisk(int mode)
    {
        // 从工厂生成一个飞碟
        GameObject disk = diskFactory.GetDisk(round);
        // 设置飞碟的随机位置
        disk.transform.position = new Vector3(-disk.GetComponent<DiskData>().direction.x * 7, UnityEngine.Random.Range(0f, 8f), 0);
        disk.SetActive(true);
        // 设置飞碟的飞行动作
        if (mode == 0){
            actionManager.Fly(disk, disk.GetComponent<DiskData>().speed*0.5f, disk.GetComponent<DiskData>().direction);
        }
        else if (mode == 2) {
            actionManager.Fly(disk, disk.GetComponent<DiskData>().speed*1.5f, disk.GetComponent<DiskData>().direction);
        }
        else {
            actionManager.Fly(disk, disk.GetComponent<DiskData>().speed, disk.GetComponent<DiskData>().direction);
        }
        
    }

    void Update()
    {
        sendTime += Time.deltaTime;
        // 每隔1s发送一次飞碟
        if (sendTime > 1)
        {
            sendTime = 0;
            // 每次发送至多5个飞碟
            for (int i = 0; i < 5 && sendCount < roundDisks[round-1]; i++)
            {
                sendCount++;
                SendDisk(mode);
            }
            // 判断是否需要重置轮次，不需要则输出游戏结束
            if (sendCount == roundDisks[round-1] && round == roundDisks.Length)
            {
                if (mode == 3)
                {
                    round = 1;
                    sendCount = 0;
                    userGUI.SetResult("");
                }
                else
                {
                    userGUI.SetResult("游戏结束！");
                }
            }
            // 更新轮次
            if (sendCount == roundDisks[round-1] && round < roundDisks.Length)
            {
                sendCount = 0;
                round++;
                userGUI.SetRound(round);
            }
        }
    }
}
